package com.wbdgj.ui.store;

import android.content.Context;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.CardView;
import android.view.View;

import com.wbdgj.R;
import com.wbdgj.base.BaseActivity;
import com.wbdgj.ui.store.cafe.StoreCofeFragment01;
import com.wbdgj.ui.store.cafe.StoreCofeFragment02;
import com.wbdgj.ui.store.cafe.StoreCofeFragment03;
import com.wbdgj.ui.store.cafe.StoreCofeFragment04;
import com.wbdgj.views.EasyRadioGroup;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class StoreCofeActivity extends BaseActivity {

    private Context context = this;
    private String bId;

    private static EasyRadioGroup easyRadioGroup;
    private StoreCofeFragment01 storyCofeFragment01;
    private StoreCofeFragment02 storyCofeFragment02;
    private StoreCofeFragment03 storyCofeFragment03;
    private StoreCofeFragment04 storyCofeFragment04;

    List<Fragment> fragments;
    Fragment currentFragment;

    @Override
    protected int getLayoutResId() {
        return R.layout.ui_story_cofe;
    }

    @Override
    protected void initView() {
        easyRadioGroup = (EasyRadioGroup) findViewById(R.id.home_bottom);
        fragments = new ArrayList<>();
        //初始化Fragment
        fragments.add(storyCofeFragment01 = (StoreCofeFragment01) Fragment.instantiate(this, StoreCofeFragment01.class.getName()));
        fragments.add(storyCofeFragment02 = (StoreCofeFragment02) Fragment.instantiate(this, StoreCofeFragment02.class.getName()));
        fragments.add(storyCofeFragment03 = (StoreCofeFragment03) Fragment.instantiate(this, StoreCofeFragment03.class.getName()));
        fragments.add(storyCofeFragment04 = (StoreCofeFragment04) Fragment.instantiate(this, StoreCofeFragment04.class.getName()));
        currentFragment = storyCofeFragment01;
        switchFragment(null, storyCofeFragment01);
        easyRadioGroup.setOnTabSelectListener(new EasyRadioGroup.OnTabSelectListener() {
            @Override
            public void onSelect(View view, int position) {

                switchFragment(currentFragment, fragments.get(position));
                currentFragment = fragments.get(position);

            }
        });

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            CardView cardView = (CardView) easyRadioGroup.getParent();
            reflectBg(cardView);
        }
    }

    public static void  choiceFragment(int tab){
        easyRadioGroup.setTabCurrenItem(tab);
    }

    public void  switchFragment(Fragment from, Fragment to) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (from == null) {
            transaction.add(R.id.frag_container, to);
        } else {
            if (!getSupportFragmentManager().getFragments().contains(to)) {
                transaction.add(R.id.frag_container, to);
            }
            transaction.hide(from);
            transaction.show(to);
        }
        transaction.commitAllowingStateLoss();
    }

    private void reflectBg(final CardView cardView) {

        try {
            Field field = cardView.getClass().getDeclaredField("mCardViewDelegate");
            field.setAccessible(true);
            Object object = field.get(cardView);
            Method method = object.getClass().getDeclaredMethod("setShadowPadding", int.class, int.class, int.class, int.class);
            method.invoke(object, 0, 0, 0, 0);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        cardView.postDelayed(new Runnable() {
            @Override
            public void run() {
                Drawable drawable = cardView.getBackground();
                try {
                    Field field = drawable.getClass().getDeclaredField("mCardBounds");
                    field.setAccessible(true);
                    RectF rectF = (RectF) field.get(drawable);
                    rectF.set(cardView.getLeft(), cardView.getTop(), cardView.getRight(), cardView.getBottom());
                    drawable.invalidateSelf();
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }, 100);
    }
}
